package com.roden.study.java.util.stream;

import org.junit.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.Spliterator;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @Author luod
 * @create 2021/11/23 19:38
 */
public class StreamBase {
    /**
     * 可拆分迭代器Spliterator
     */
    @Test
    public  void tryAdvance() {
        List<Integer> list = new ArrayList<>();
        list.add(2);
        list.add(1);
        list.add(3);
        Spliterator<Integer> spliterator = list.stream().spliterator();
        final AtomicInteger round = new AtomicInteger(1);
        final AtomicInteger loop = new AtomicInteger(1);
        while (spliterator.tryAdvance(num -> System.out.printf("第%d轮回调Action,值:%d\n", round.getAndIncrement(), num))) {
            System.out.printf("第%d轮循环\n", loop.getAndIncrement());
        }
    }

    /**
     * 可拆分迭代器Spliterator
     */    
    @Test
    public  void forEachRemaining() {
        List<Integer> list = new ArrayList<>();
        list.add(2);
        list.add(1);
        list.add(3);
        Spliterator<Integer> spliterator = list.stream().spliterator();
        final AtomicInteger round = new AtomicInteger(1);
        spliterator.forEachRemaining(num -> System.out.printf("第%d轮回调Action,值:%d\n", round.getAndIncrement(), num));
    }

    /**
     * 可拆分迭代器Spliterator
     */
    @Test
    public  void trySplit() {
        List<Integer> list = new ArrayList<>();
        list.add(2);
        list.add(3);
        list.add(4);
        list.add(1);
        Spliterator<Integer> first = list.stream().spliterator();
        Spliterator<Integer> second = first.trySplit();
        first.forEachRemaining(num -> {
            System.out.printf("first spliterator item: %d\n", num);
        });
        second.forEachRemaining(num -> {
            System.out.printf("second spliterator item: %d\n", num);
        });
    }
}
